import by3857To4326Fn from "../../utils/proj4/index";
import superMapForCesiumBaseMap, {
  entitieAdd,
  entitieRemove,
  entitieAddLine,
} from "../../utils/globalVariable/superMapForCesiumBaseMap";
import axios from "axios";
const jhxLineSearch =
  iserverOrigin +
  "/iserver/services/spatialAnalysis-JingGuiYuanXianCan/restjsr/spatialanalyst/datasets/%E7%BA%BF%E5%8F%82%40%E7%BB%8F%E8%A7%84%E9%99%A2%E7%BA%BF%E5%8F%82/linearreferencing/routelocator.json";

const lineLocationFn = (routeID, measure, metadata, url = jhxLineSearch) => {
  axios({
    url: url,
    method: "POST",
    data: {
      routeIDField: "线编码",
      routeID: `'${routeID}'`,
      type: "POINT",
      measure: measure,
      offset: 0,
      isIgnoreGap: false,
    },
  }).then((response) => {
    if (response.data.succeed) {
      const newResourceLocation = response.data.newResourceLocation;
      axios({
        url: newResourceLocation + ".json",
        params: { returnPostAction: true },
        method: "GET",
      }).then((response) => {
        if (response.data.succeed) {
          const resultGeometry = response.data.resultGeometry;
          console.log(resultGeometry);
          const xy4326 = by3857To4326Fn(
            resultGeometry.points[0].x,
            resultGeometry.points[0].y
          );
          console.log(xy4326);
          entitieAdd(
            superMapForCesiumBaseMap.viewer,
            xy4326[0],
            xy4326[1],
            true,
            metadata
          );
        }
      });
    }
  });
};

window._lineLocationFn = lineLocationFn;

